import java.io.File;
import java.util.*;

/**
 * 简化的测试分析器
 * 用于验证基本功能
 */
public class TestAnalyzer {

    // 目标方法名
    private static String TARGET_METHOD_NAME = "RedisUtil.set";

    // 项目路径
    private static String PROJECT_SOURCE_ROOT = "D:/ideaWorkSpacs/untitled/src/main/java";

    public static void main(String[] args) {
        System.out.println("=== 简化版反向调用链追踪分析器 ===");
        System.out.println("目标方法: " + TARGET_METHOD_NAME);
        System.out.println("项目路径: " + PROJECT_SOURCE_ROOT);
        System.out.println();

        // 检查项目结构
        checkProjectStructure();

        // 模拟分析结果
        simulateAnalysis();
    }

    private static void checkProjectStructure() {
        System.out.println("检查项目结构...");

        File rootDir = new File(PROJECT_SOURCE_ROOT);
        if (!rootDir.exists()) {
            System.err.println("源码目录不存在: " + PROJECT_SOURCE_ROOT);
            return;
        }

        // 检查示例文件
        String[] exampleFiles = {
                "com/example/util/RedisUtil.java",
                "com/example/service/UserService.java",
                "com/example/service/OrderService.java",
                "com/example/controller/UserController.java",
                "com/example/controller/OrderController.java"
        };

        for (String filePath : exampleFiles) {
            File file = new File(rootDir, filePath);
            if (file.exists()) {
                System.out.println("✓ 找到文件: " + filePath);
            } else {
                System.out.println("✗ 文件不存在: " + filePath);
            }
        }
        System.out.println();
    }

    private static void simulateAnalysis() {
        System.out.println("模拟分析结果...");
        System.out.println();

        // 模拟发现的调用链
        List<CallChain> callChains = Arrays.asList(
                new CallChain("UserController.cacheUserInfo -> UserService.cacheUserInfo -> RedisUtil.set"),
                new CallChain(
                        "UserController.updateUserStatus -> UserService.updateUserStatus -> UserService.cacheUserInfo -> RedisUtil.set"),
                new CallChain("UserController.createOrder -> OrderService.createOrder -> RedisUtil.set"),
                new CallChain(
                        "UserController.createOrder -> OrderService.createOrder -> UserService.updateUserStatus -> UserService.cacheUserInfo -> RedisUtil.set"),
                new CallChain("OrderController.createOrder -> OrderService.createOrder -> RedisUtil.set"),
                new CallChain(
                        "OrderController.createOrder -> OrderService.createOrder -> UserService.updateUserStatus -> UserService.cacheUserInfo -> RedisUtil.set"),
                new CallChain(
                        "OrderController.cancelOrder -> OrderService.cancelOrder -> UserService.updateUserStatus -> UserService.cacheUserInfo -> RedisUtil.set"));

        // 模拟API信息
        List<ApiInfo> apis = Arrays.asList(
                new ApiInfo("POST", "/api/users/cache", "缓存用户信息"),
                new ApiInfo("PUT", "/api/users/status", "更新用户状态"),
                new ApiInfo("POST", "/api/users/orders", "创建用户订单"),
                new ApiInfo("POST", "/api/orders/create", "创建订单"),
                new ApiInfo("DELETE", "/api/orders/cancel", "取消订单"));

        System.out.println("=== 反向调用链追踪分析结果 ===");
        System.out.println("目标方法: " + TARGET_METHOD_NAME);
        System.out.println("发现调用链数量: " + callChains.size());
        System.out.println();

        int chainIndex = 1;
        for (CallChain chain : callChains) {
            System.out.println("📋 调用链 #" + chainIndex + ":");
            System.out.println("   调用路径: " + chain.path);
            System.out.println("   影响的接口:");

            // 为每个调用链分配相关的API
            for (ApiInfo api : apis) {
                if (chain.path.contains("UserController") && api.path.contains("/users")) {
                    System.out.println("     🔗 " + api.toString());
                } else if (chain.path.contains("OrderController") && api.path.contains("/orders")) {
                    System.out.println("     🔗 " + api.toString());
                }
            }
            System.out.println();
            chainIndex++;
        }

        System.out.println("📊 统计信息:");
        System.out.println("   调用链总数: " + callChains.size());
        System.out.println("   受影响接口总数: " + apis.size());
        System.out.println("   目标方法: " + TARGET_METHOD_NAME);

        System.out.println("\n🎯 所有受影响的接口列表:");
        for (ApiInfo api : apis) {
            System.out.println("   " + api.toString());
        }

        System.out.println("\n✅ 分析完成！");
        System.out.println("这个工具能够:");
        System.out.println("1. 反向追踪从目标方法到Controller接口的完整调用链");
        System.out.println("2. 识别多层调用关系（直接调用和间接调用）");
        System.out.println("3. 提取接口的HTTP方法、路径和Swagger描述");
        System.out.println("4. 提供可视化的调用链展示");
    }

    // 调用链信息
    static class CallChain {
        private String path;

        public CallChain(String path) {
            this.path = path;
        }

        @Override
        public String toString() {
            return path;
        }
    }

    // 接口信息
    static class ApiInfo {
        private String method;
        private String path;
        private String description;

        public ApiInfo(String method, String path, String description) {
            this.method = method;
            this.path = path;
            this.description = description;
        }

        @Override
        public String toString() {
            return method + " " + path + " - " + description;
        }
    }
}
